package com.huan.service.impl;

import com.huan.dao.mapper.StudentMapper;
import com.huan.pojo.Count;
import com.huan.pojo.PageBean;
import com.huan.pojo.Student;
import com.huan.service.StudentService;
import com.huan.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


import java.awt.print.Book;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    //1.创建工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    //分页条件查询
    @Override
    public PageBean<Student> selectByPageAndCondition(int currentPage, int pageSize, Student student) {
        SqlSession sqlSession = factory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //计算开始索引
        int begin = (currentPage - 1)*pageSize;
        //计算查询条目数
        int size = pageSize;

        String name = student.getName();
        String studentID = student.getStudentID();
        if (name!=null&&name.length()>0){
            student.setName("%"+name+"%");
        }else if (studentID!=null&&studentID.length()>0){
            student.setStudentID("%"+studentID+"%");
        }

        //查询当前页数据
        List<Student> rows = mapper.selectByPageAndCondition(begin, size, student);

        //查询总记录数
        int totalCount = mapper.selectTotalCountByCondition(student);

        //封装PageBean对象
        PageBean<Student> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();

        return pageBean;
    }

    //添加
    @Override
    public boolean add(Student student) {
        SqlSession sqlSession = factory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        boolean flag = mapper.add(student);
        sqlSession.commit();
        sqlSession.close();
        return flag;
    }

    //编辑
    @Override
    public int update(Student student) {
        SqlSession sqlSession = factory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int i = mapper.update(student);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    //删除
    @Override
    public void delete(String studentID) {
        SqlSession sqlSession = factory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.delete(studentID);
        sqlSession.commit();
        sqlSession.close();
    }

    //批量删除
    @Override
    public void deleteByIds(String[] ids) {
        SqlSession sqlSession = factory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.deleteByIds(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Student> printStudent() {
        SqlSession sqlSession = factory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.printStudent();
        sqlSession.close();
        return students;
    }


}
